programming4us
           
 
 
Programming

Software Testing with Visual Studio Team System 2008 : Unit testing an ASP.NET application

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
8/8/2011 6:09:28 PM
Creating the unit test for an ASP.NET web site or application is similar to the one we created for the normal class library. The ASP.NET unit test is used for testing the methods or the business logic used for the ASP.NET site. The only difference is the additional attributes added to the methods to identify the URL and the Host. The ASP.NET unit test can be run using IIS web server or the development web server. If it is on the IIS server we can choose the user identity with which the unit test should run. The default identity depends on the version of the IIS server and the operating system.

Let us consider a simple user registration page created using ASP.NET using Visual Studio 2008. Below is the UI for the user to get registered.

This web application runs on the local development server. The application has two methods. One is to get the user details from the user interface and create a new user object and the other is to just display the user name on the screen after submit. The application also has a class file for the user information.

protected void BtnSubmit_Click(object sender, EventArgs e)
unit testingfor ASP.NET application{
User usr = new User();
GetUserDetails(usr);
LabelOutput.Text = "Hello " + usr.FirstName + " " +
usr.LastName + " you are sucessfully registered with the site";
}
public User GetUserDetails(User user)
{
user.FirstName = TextBoxFirstName.Text;
user.LastName = TextBoxLastName.Text;
user.MiddleName = TextBoxMiddleName.Text;
user.Address = TextBoxCity.Text;
user.Street = TextBoxStreet.Text;
user.City = TextBoxCity.Text;
user.Country = TextBoxCountry.Text;
user.Email = TextBoxEmail.Text;
user.Phone = TextBoxPhone.Text;
return user;
}

Before generating the unit test for the web application, let us build and run the application once to make sure it runs as expected.

Now to generate the unit test, open the code file of the web page, then right-click and select the Create Unit Tests… option which identifies all the classes and the methods for which the unit test can be generated as shown in the screenshot below:

Select the methods and the user class for which the unit test can be generated and tested. Now Visual Studio creates the unit test class file for the new test with the required attributes and the base code for test. The unit test code for the two methods of the web page would be:

[TestMethod()]
unit testingfor ASP.NET application[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\\Workspace\\UnitTest\\ SampleAppforUnitTest\\SampleAppforUnitTest", "/")]
[UrlToTest("http://localhost:11961/")]
[DeploymentItem("SampleAppforUnitTest.dll")]
public void BtnSubmit_ClickTest()
{
_Default_Accessor target = new _Default_Accessor(); // TODO: Initialize to an appropriate value
object sender = null; // TODO: Initialize to an //appropriate value
EventArgs e = null; // TODO: Initialize to an //appropriate value
target.BtnSubmit_Click(sender, e);
Assert.Inconclusive("A method that does not return a value cannot be verified.");
}
///
///A test for GetUserDetails
///
[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\\Workspace\\UnitTest\\ SampleAppforUnitTest\\SampleAppforUnitTest", "/")]
[UrlToTest("http://localhost:11961/")]
public void GetUserDetailsTest()
{
_Default target = new _Default(); // TODO: Initialize //to an appropriate value
User user = null; // TODO: Initialize to an //appropriate value
User expected = null; // TODO: Initialize to an //appropriate value
User actual;
actual = target.GetUserDetails(user);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}




There are different attributes added to the unit test methods:

  • TestMethod—attribute to identify the whether a method is a unit test method.

  • HostType—the type of the host that takes care of running the unit test. In this case it is the ASP.NET.

  • AspNetDevelopmentServerHost—this specifies the settings to be used when the development server is used as the host for the unit testing.

  • UrlToTest—specifies the application URL to be used for the test context.

  • DeploymentItem—this is to specify the items like files and folders to be deployed before the test.

After creating the test and setting the environment, set the expected values and include the required assert method for comparing the values and passing the test. For example, change the GetUserDetailsTest method as below:

[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\\Workspace\\UnitTest\\ SampleAppforUnitTest\\SampleAppforUnitTest", "/")]
[UrlToTest("http://localhost:11961/")]
public void GetUserDetailsTest()
{
_Default target = new _Default();
User user = new User();
User expected = new User();
expected.FirstName = "Subha";
expected.LastName = "S";
User actual;
actual = target.GetUserDetails(user);
Assert.AreEqual(expected.FirstName, actual.FirstName);
}



The test will pass if the value of FirstName and LastName properties of the expected object and the actual object returned by the method are equal. Right-click on the method and select Run Tests...

We can use the different assert methods and test all different scenarios of the unit test with different expected values.

Other -----------------
- Microsoft Enterprise Library : Error Management Made Exceptionally Easy - Replacing an Exception & Logging an Exception
- Microsoft Enterprise Library : Error Management Made Exceptionally Easy - Diving in with a Simple Example
- iPhone Programming : Connecting to the Network - Embedding a Web Browser in Your App
- iPhone Programming : Connecting to the Network - Detecting Network Status
- Parallel Programming with Microsoft Visual Studio 2010 : Introduction to Parallel Programming - Software Patterns
- Parallel Programming with Microsoft Visual Studio 2010 : Introduction to Parallel Programming - Multicore Computing & Speedup
- Microsoft ASP.NET 3.5 : Web Services for ASP.NET AJAX Applications (part 2) - Consuming AJAX Web Services
- Microsoft ASP.NET 3.5 : Web Services for ASP.NET AJAX Applications (part 1) - Remote Calls via Web Services
- Microsoft ASP.NET 3.5 : AJAX-Enabled Web Services - Implementing the AJAX Paradigm
- The Art of SEO : Measuring Search Traffic (part 2)
- The Art of SEO : Measuring Search Traffic (part 1)
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Do Math
- Programming Excel with VBA and .NET : Tasks in Visual Basic - Interact with Users
- Context and Interception : The .NET Context
- Context and Interception : .NET Component Services
- Optimizing for Vertical Search : Mobile, Video & Multimedia Search
- Programming WCF Services : Data Contracts - Collections (part 2) - The CollectionDataContract Attribute & Dictionaries
- Programming WCF Services : Data Contracts - Collections (part 1) - Concrete Collections & Custom Collections
- iPhone Programming : The Image Picker View Controller - Adding the Image Picker to the City Guide Application
- iPhone Programming : Other View Controllers - Modal View Controllers
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us